package com.carbeauty.scan;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItem;

import com.carbeauty.image.Constants;
public class GetScanFileServlet extends HttpServlet {
	private static final long serialVersionUID = -167798455203829190L;
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getSession().removeAttribute("__fileScanName");
		 saveFile(request); 
	}
 
	@SuppressWarnings("deprecation")
	private void saveFile(HttpServletRequest request)
			throws IOException {
		String destFolder = Constants.FILE_UPLOAD_TEMP_DIR;
		if (!destFolder.matches(".*(\\\\|/)$"))
			destFolder += "/";
		DiskFileUpload fileUpload = new DiskFileUpload();
		fileUpload.setHeaderEncoding("GBK");
		try {
			List fileList = fileUpload.parseRequest(request);
			DiskFileItem file = (DiskFileItem) fileList.get(0);
			String clientFileName = file.getName();
			String extName = clientFileName.substring(clientFileName
					.lastIndexOf("."));
			String primaryKey =    "Temp_"+UUID.randomUUID().toString();  ;//cn.chinaclear.sz.base.PrimaryKey.getPrimaryKey();
			String fileName=  primaryKey + extName;
			File fileToRecv = new File(destFolder + primaryKey + extName);
			file.write(fileToRecv);
			request.getSession().setAttribute("__fileScanName", fileName);
		} catch (Exception e) {
			throw new RuntimeException("文件上传失败...", e);
		}
 
	}
}
